package com.p7.notepad.tools;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.p7.notepad.app.MyApplication;
import com.p7.notepad.bean.Login;
import com.p7.notepad.bean.User;
import com.tools.s7.utils.SharedPreferencesHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administor on 2018\3\31 0031.
 */
public class Storage {

    private static final String USER_LIST = "user_list";
    private static final String ID = "id";
    private static final String USER = "user";
    private static final String IMAGE = "image";
    private static final String NAME = "name";
    private static final String MODE = "mode";

    public static void putUser(Login.DataBean user) {
        String value = new Gson().toJson(user);
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putStringValue(USER, value);
    }
    public static Login.DataBean getUser() {
        return new Gson().fromJson(SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getStringValue(USER), Login.DataBean.class);
    }

    public static void putImage(String image) {
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putStringValue(IMAGE, image);
    }
    public static String getImage() {
        return SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getStringValue(IMAGE);
    }
    public static boolean isImage() {
        return SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).isContains(IMAGE);
    }

    public static void putId(int id) {
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putIntValue(ID, id);
    }
    public static int getId() {
        return SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getIntValue(ID);
    }
    public static boolean isId() {
        return SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).isContains(ID);
    }

    public static void putName(String name) {
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putStringValue(NAME, name);
    }
    public static String getName() {
        return SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getStringValue(NAME);
    }
    public static boolean isName() {
        return SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).isContains(NAME);
    }

    public static void putMode(boolean mode) {
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putBooleanValue(MODE, mode);
    }
    public static boolean getMode() {
        return SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getBooleanValue(MODE);
    }

    public static void putUserName(int id, String name) {
        List<User> data = new ArrayList<>();
        String content = SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getStringValue(USER_LIST);
        Gson gson = new Gson();
        if (!TextUtils.isEmpty(content)) {
            data = gson.fromJson(content, new TypeToken<ArrayList<User>>() {}.getType());
            for (User s : data) {
                if (s.getId() == id) {
                    s.setNickName(name);
                }
            }
        }
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putStringValue(USER_LIST, gson.toJson(data));
    }

    public static void putUserUrl(int id, String url) {
        List<User> data = new ArrayList<>();
        String content = SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getStringValue(USER_LIST);
        Gson gson = new Gson();
        if (!TextUtils.isEmpty(content)) {
            data = gson.fromJson(content, new TypeToken<ArrayList<User>>() {}.getType());
            for (User s : data) {
                if (s.getId() == id) {
                    s.setAvatar(url);
                }
            }
        }
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putStringValue(USER_LIST, gson.toJson(data));
    }

    public static void putUserList(User user) {
        List<User> data = new ArrayList<>();
        String content = SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getStringValue(USER_LIST);
        Gson gson = new Gson();
        if (TextUtils.isEmpty(content)) {
            data.add(user);
        } else {
            data = gson.fromJson(content, new TypeToken<ArrayList<User>>() {}.getType());
            boolean is = false;
            for (User s : data) {
                if (s.getMobile().equals(user.getMobile())) {
                    is = true;
                }
            }
            if (!is) {
                data.add(user);
            }
        }
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putStringValue(USER_LIST, gson.toJson(data));
    }
    public static List<User> getUserList() {
        String content = SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).getStringValue(USER_LIST);
        if (TextUtils.isEmpty(content))
            return new ArrayList<>();
        Gson gson = new Gson();
        return gson.fromJson(content, new TypeToken<ArrayList<User>>(){}.getType());
    }

    public static boolean isLogin() {
        return SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).isContains(ID);
    }

    public static void onLogout() {
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).removeSetting(ID);
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).removeSetting(USER);
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).removeSetting(IMAGE);
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).removeSetting(NAME);
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).putBooleanValue(MODE, false);
    }

    public static void delAll() {
        SharedPreferencesHelper.getInstance(MyApplication.getInstance().getApplicationContext()).clear();
    }

}
